package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.WechatRole;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.bean.WechatUserRole;
import com.jy.adscm.wechat.mapper.mysql.WechatRoleMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserRoleMapper;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-24
 */
@Service
public class WechatUserRoleServiceImpl extends ServiceImpl<WechatUserRoleMapper, com.jy.adscm.wechat.bean.WechatUserRole> implements WechatUserRoleService {


     @Autowired
    WechatUserMapper wechatUserMapper;
     @Autowired
    WechatUserRoleMapper wechatUserRoleMapper;
     @Autowired
    WechatRoleMapper wechatRoleMapper;
    @Override
    public List<WechatRole> selectByOpenId(String openid) throws AdscmSystemException{
        List<WechatRole> wechatRoles=new ArrayList<>();
       //根据openid拿到这个用的用户信息
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            throw new AdscmSystemException("您还未登录注册");
        }
        //查询这个用户的角色信息
        Wrapper<WechatUserRole> wechatUserRoleWrapper=new EntityWrapper<>();
        wechatUserRoleWrapper.eq("user_id",wechatUser.getId());
        List<WechatUserRole> wechatUserRoles = wechatUserRoleMapper.selectList(wechatUserRoleWrapper);
            for (WechatUserRole wechatUserRole : wechatUserRoles) {
                WechatRole wechatRole = wechatRoleMapper.selectById(wechatUserRole.getRoleId());
                wechatRoles.add(wechatRole);
            }
        return wechatRoles;
    }

    @Override
    public Boolean isJzUser(String openid) throws AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            throw new AdscmSystemException("您还未登录注册");
        }
        Wrapper<WechatUserRole> wechatUserRoleWrapper=new EntityWrapper<>();
        wechatUserRoleWrapper.eq("user_id",wechatUser.getId());
        List<WechatUserRole> wechatUserRoles = wechatUserRoleMapper.selectList(wechatUserRoleWrapper);
        Boolean isJzUser=false;
        for (WechatUserRole wechatUserRole : wechatUserRoles) {
           if (wechatUserRole.getRoleId()==1){
               isJzUser=true;
           }
        }
        return isJzUser;
    }

    @Override
    public Boolean userHasRole(String userId, String RoleId) {
        Boolean result=false;
        Wrapper<WechatUserRole> wrapper=new EntityWrapper<>();
        wrapper.eq("role_id",RoleId);
        wrapper.eq("user_id",userId);
        List<WechatUserRole> wechatUserRoles = super.selectList(wrapper);
        if (wechatUserRoles!=null&&wechatUserRoles.size()>0){
            result=true;
        }
        return result;
    }
}
